package com.leoandroid.tool.toolsbox.new

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.leoandroid.tool.toolsbox.adapter.NBAMessageAdapter
import com.leoandroid.tool.toolsbox.bean.NBAInfoBean
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityNbainfoBinding
import com.leoandroid.tool.toolsbox.net.NetCallBack
import com.leoandroid.tool.toolsbox.net.NetRequest

import org.json.JSONObject
//NBA赛事
class NBAInfoActivity : AppCompatActivity() {
    private var dataBind: ActivityNbainfoBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBind = ActivityNbainfoBinding.inflate(layoutInflater)

        setContentView(dataBind?.root)
        initView()
    }

    private fun initView() {

        dataBind?.apply {
            titleBack.setOnClickListener { finish() }
        }
        val url = "http://apis.juhe.cn/fapig/nba/query?key=2877d7b59e20804ace4d4a40c58f0729"
        NetRequest.asyncGet(url,object: NetCallBack{
            override fun onFailure(code: Int, e: Throwable?) {

            }

            override fun onSuccess(json: JSONObject?) {
                val string = json?.getString("result")
                val jsonResult = JSONObject(string)
                val string1 = jsonResult.getString("matchs")
                val type = object : TypeToken<ArrayList<NBAInfoBean>>() {}.type
                val fromJson = Gson().fromJson<ArrayList<NBAInfoBean>>(string1, type)
                this@NBAInfoActivity.runOnUiThread {
                    val adapter: NBAMessageAdapter = NBAMessageAdapter(this@NBAInfoActivity,fromJson)
                    dataBind?.nbaList?.layoutManager = LinearLayoutManager(this@NBAInfoActivity)
                    dataBind?.nbaList?.adapter = adapter
                }
            }

        })
    }
}